﻿using System;
using System.Windows.Input;
using ClientOverflow.Core;
using ClientOverflow.DataTransferObjects;
using ClientOverflow.ViewModels.Services;

namespace ClientOverflow.ViewModels.QuestionViewViewModels
{
    /// <summary>
    /// VoteQuestionCommandBase is an abstract command for voting a question up or down. This class provides
    /// most of the implementation that initiates the connection to the WCF service that logs the votes.
    /// The Execute method implementats the template method pattern. The actual implementation that fills the
    /// temaplte is located in VoteQuestionUpCommand and VoteQuestionDownCommand classes
    /// </summary>
    public abstract class VoteQuestionCommandBase : ICommand
    {
        protected readonly Question question;
        public event Action Executed = () => { };

        protected VoteQuestionCommandBase(Question question)
        {
            this.question = question;
        }

        public void Execute(object parameter)
        {
            using (var serviceClient = ServicesFactory.CreateClientOverflowServiceClient())
            {
                var loginService = ServicesProviderFactory.GetInstance().LoginService;
                var votingUser = loginService.LoggenInUser;
                Vote(serviceClient, votingUser);
            }
            Executed();
        }

        protected abstract void Vote(IClientOverflowService serviceClient, User votingUser);

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}